package com.bedrock;

import com.alibaba.fastjson.JSONArray;
import com.bedrock.framework.security.service.SysLoginService;
import com.bedrock.project.inside.service.WorkHourService;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.DateFormat;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApiTestUtil extends AbstractJUnit4SpringContextTests {

    public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));

    @Autowired
    private  SysLoginService loginService;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private WorkHourService workHourService;


    protected MockMvc mockMvc;
    protected MockHttpSession session;

    @Before
    public void setUp() {

        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();//建议使用这种
//        session = new MockHttpSession(); //2.初始化
//        // 登陆
//        MockHttpServletRequestBuilder loginRequestBuilder = MockMvcRequestBuilders.post("/auth/login")
//                .param("username", "admin")
//                .param("password", "admin123")
//                .contentType(MediaType.APPLICATION_JSON_UTF8)
//                .accept(MediaType.APPLICATION_JSON)
//                .session(session);//3.当某个请求需要session时，直接在构造器中绑定需要的session

    }

    public  String readJsonData(String filePath){
        InputStream inputStream = this.getClass().getResourceAsStream(filePath);
        String jsonData = "";
        try {
            jsonData = IOUtils.toString(inputStream, Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonData;
    }

    //获取token
    protected String getToken(){
     return loginService.getToken("admin","admin123");
    }



    @Test
    public void testworkHour(){
        workHourService.sendMailWeekReport();
    }



}
